/**
 * Created by hkf6644 on 2017/5/25.
 */
var userAddObj = {
    addParamObj : {
        userId : '',
        loginId : '',
        name : '',
        password : '',
        confirmPassword : '',
        idNumber : '',
        mobilephone : '',
        email : '',
        roleId : '',
        ouId : '',
        deptId : ''
    },

    password : '',
    confirmPassword : '',
    idNumber : '',
    mobilephone : '',
    email : '',
    orgId : '',
    deptValue : [],

    deptData : [],
    orgData : [],
    roleData : []
};

var initAddUserData = function (isEditor) {
    if (!isEditor) {
        userAddObj.addParamObj = {
            userId : '',
            loginId : '',
            name : '',
            password : '',
            confirmPassword : '',
            idNumber : '',
            mobilephone : '',
            email : '',
            roleId : '',
            ouId : '',
            deptId : ''
        };

        userAddObj.password = '';
        userAddObj.confirmPassword = '';
        userAddObj.idNumber = '';
        userAddObj.mobilephone = '';
        userAddObj.email = '';
        userAddObj.orgId = '';
        userAddObj.deptValue = [];
    }

    getOrgList(true);
    getRoleList(true);
};

Vue.component('createNewUser', {
    props: ['modal'],
    template : CommonFunc.ajax.getOuterHtml('component/user_add.html'),
    data: function () {
        return userAddObj
    },
    watch : {
        orgId : function () {
            this.addParamObj.ouId = this.orgId;
            getDeptList(this.orgId);
        },
        deptValue : function () {
            this.addParamObj.deptId = this.deptValue[this.deptValue.length - 1];
            if (this.addParamObj.deptId) {
                vmdata.warningAlert = '';
            }
        }
    },
    methods: {
        passwordChange : function () {
            this.addParamObj.password = this.password;
        },
        confirmPasswordChange : function () {
            if (this.password !== this.confirmPassword) {
                vmdata.warningAlert = '两次输入密码不一致，请重新输入！';
            } else {
                vmdata.warningAlert = '';
            }

            this.addParamObj.confirmPassword = this.confirmPassword;
        },
        idNumberChange : function () {
            var reg=/^[1-9]{1}[0-9]{14}$|^[1-9]{1}[0-9]{16}([0-9]|[xX])$/;
            if(!reg.test(this.idNumber)){
                vmdata.warningAlert = '身份证号格式不正确！';
            } else {
                vmdata.warningAlert = '';
            }

            this.addParamObj.idNumber = this.idNumber;
        },
        mobilePhoneChange : function () {
            if(!/^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$/i.test(this.mobilephone)){
                vmdata.warningAlert = '手机号格式不正确！';
            } else {
                vmdata.warningAlert = '';
            }

            this.addParamObj.mobilephone = this.mobilephone;
        },
        emailChange : function () {
            var filter  = /^([a-zA-Z0-9\_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            if (!filter.test(this.email)){
                vmdata.warningAlert = '邮箱格式不正确！';
            } else {
                vmdata.warningAlert = '';
            }

            this.addParamObj.email = this.email;
        },
        searchImf : function () {

        },
        saveUser : function () {
            var _this = this;

            if (!_this.addParamObj.loginId) {
                vmdata.warningAlert = '请输入登录账号！';
                return false;
            }

            if (!_this.addParamObj.name) {
                vmdata.warningAlert = '请输入姓名！';
                return false;
            }

            if (!_this.addParamObj.userId) {
                if (!_this.addParamObj.password) {
                    vmdata.warningAlert = '请输入密码！';
                    return false;
                }
                if (_this.addParamObj.password !== _this.addParamObj.confirmPassword) {
                    vmdata.warningAlert = '两次输入密码不一致，请重新输入！';
                    return false;
                }
            }

            if (!_this.addParamObj.idNumber) {
                vmdata.warningAlert = '请输入身份证号！';
                return false;
            }
            var reg=/^[1-9]{1}[0-9]{14}$|^[1-9]{1}[0-9]{16}([0-9]|[xX])$/;
            if(!reg.test(this.addParamObj.idNumber)){
                vmdata.warningAlert = '身份证号格式不正确！';
                return false;
            }

            if (!_this.addParamObj.mobilephone) {
                vmdata.warningAlert = '请输入手机号！';
                return false;
            }
            if(!/^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$/i.test(this.addParamObj.mobilephone)){
                vmdata.warningAlert = '手机号格式不正确！';
                return false;
            }

            if (!_this.addParamObj.email) {
                vmdata.warningAlert = '请输入邮箱！';
                return false;
            }
            var filter  = /^([a-zA-Z0-9\_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            if (!filter.test(this.addParamObj.email)){
                vmdata.warningAlert = '邮箱格式不正确！';
                return false;
            }

            if (!_this.addParamObj.roleId) {
                vmdata.warningAlert = '请选择角色！';
                return false;
            }

            if (!_this.addParamObj.ouId) {
                vmdata.warningAlert = '请选择组织！';
                return false;
            }

            if (!_this.addParamObj.deptId) {
                vmdata.warningAlert = '请选择部门！';
                return false;
            }

            if (_this.addParamObj.userId) {
                var saveUrl = '/cas/console/user/edit.ajax';
            } else {
                var saveUrl = '/cas/console/user/add.ajax';
            }
            
            var saveCallback = function (rs) {
                if (rs.isSuccess) {
                    if (_this.addParamObj.userId) {
                        vmdata.successAlert = '编辑用户成功！';
                    } else {
                        vmdata.successAlert = '新建用户成功！';
                    }

                    getUserList.call(_this, 1);

                	vmdata.modal.title = '';
                    vmdata.modal.showUser = false;
                } else {
                    vmdata.errorAlert = rs.errMsg;
                }
            };

            $.ajax({
                type: "POST",
                url: saveUrl,
                async: true,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(_this.addParamObj),
                success: saveCallback
            });
        },
        cancleUser : function () {
            vmdata.modal.title = '';
            vmdata.modal.showUser = false;
        },
        calHeight : function () {
        	var wdHeight = window.innerHeight;
        	var height = $('#userAddBox').height();
        	var chazhi = wdHeight - height;
        	
        	if (wdHeight - height > 30) {
        		var top = chazhi / 2;
        		var boxHeight = height;
        	} else {
        		var top = 15;
        		var boxHeight = wdHeight - 30;
        	}
        	
        	$('#userAddTop').css({
        		'top' : top + 'px'
        	});
        	$('#userAddBox').height(boxHeight);
        	$('.userAddBox').height(wdHeight);
        }
    },
    mounted : function () {
    	this.calHeight();
    }
});